home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Mail / MailEnclosure / Source.v0.15 / Directory.m < prev    next >
Text File  |  1995-06-12  |  1KB  |  85 lines

  1. #import <regex.h>
  2. #import <sys/types.h>
  3. #import <pwd.h>
  4. #import <sys/dir.h> 
  5. #import "StringStorage.h"
  6. #import "Directory.h"
  7.  
  8. @implementation Directory
  9.  
  10. - init
  11. {
  12.    [super init];
  13.    path = [[StringStorage alloc] init];
  14.    return self;
  15. }
  16.  
  17. - init: (const char *)aPath
  18. {
  19.    [self init];
  20.    [self setPath: aPath];
  21.    return self;
  22. }
  23.  
  24. - free
  25. {
  26.    [path free];
  27.    [self freeObjects];
  28.    return [super free];
  29. }
  30.  
  31. - setPath: (const char *)str
  32. {
  33.    char user[9];
  34.    char fullPath[MAXPATHLEN + 1];
  35.    DIR *dir;
  36.    struct direct *entry;
  37.  
  38.    [path empty];
  39.    [self freeObjects];
  40.  
  41.    if(str && *str == '~')             /* used ~user or ~/ */
  42.    {
  43.       struct passwd *pw;
  44.       int x;
  45.  
  46.       for(x = 1; str[x] && x < 9; x++)
  47.       if(str[x] == '/')
  48.           break;
  49.       else
  50.           user[x - 1] = str[x];
  51.       
  52.       user[x - 1] = (char)0;
  53.       if(!strlen(user))                 /* was ~/ */
  54.       strcpy(user, NXUserName());
  55.  
  56.       pw = getpwnam(user);
  57.       if(pw)
  58.       {
  59.      strcpy(fullPath,pw->pw_dir);
  60.      strcat(fullPath,str + x);
  61.      str = fullPath;
  62.       }
  63.    }
  64.    
  65.    dir = opendir(str);                 /* open the resulting directory */
  66.  
  67.    if(!dir)                     /* failed to open */
  68.        return nil;
  69.  
  70.    [path setStringValue: str];
  71.    while(entry = readdir(dir))
  72.        [self addObject: [[StringStorage alloc] init: entry->d_name]];
  73.    closedir(dir);
  74.    return self;
  75. }
  76.  
  77.  
  78. - (const char *)path
  79. {
  80.    return [path stringValue];
  81. }
  82.  
  83.  
  84. @end
  85.